package com.project.medical.sys.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.project.medical.sys.common.utils.PageUtils;
import com.project.medical.sys.common.utils.R;
import com.project.medical.sys.entity.TokenEntity;
import com.project.medical.sys.util.Encoder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.project.medical.sys.entity.ManagerEntity;
import com.project.medical.sys.service.ManagerService;




/**
 * 
 *
 * @author wjl
 * @email ${email}
 * @date 2021-06-20 11:46:09
 */
@RestController
@RequestMapping("sys/manager")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sys:manager:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = managerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("sys:manager:info")
    public R info(@PathVariable("id") Integer id){
		ManagerEntity manager = managerService.getById(id);

        return R.ok().put("manager", manager);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sys:manager:save")
    public R save(@RequestBody ManagerEntity manager){
        ManagerEntity byId = managerService.getByPhone(manager.getPhone());
        if (byId !=null){
            return R.error("该管理员已存在");
        }
        managerService.save(manager);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sys:manager:update")
    public R update(@RequestBody ManagerEntity manager){
        boolean b = managerService.updateById(manager);
        if (b){
            TokenEntity token = new TokenEntity();
            BeanUtils.copyProperties(manager,token);
            token.setPassword(Encoder.encoder(manager.getPassword()));
            token.setType("admin");
            return R.ok().put("token",token);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sys:manager:delete")
    public R delete(@RequestBody Integer[] ids){
		managerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
